using System;
using MonoTouch.Dialog;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;


namespace Socialite
{
	// 
	// A MonoTouch.Dialog.Element that renders a PlaceDetailsCheckinCell
	//
	[Register ("PlaceDetailsCheckinElement")]
	public class PlaceDetailsCheckinElement : Element , IElementSizing
	{
		static NSString key = new NSString ("placedetailscheckinelement");
		public Place Place;
		float _cellHeight;
		
		public PlaceDetailsCheckinElement (Place place) : base (null)
		{
			Place = place;	
		}
	
		/// <summary>
		/// This function is called by the container TableView to get a cell to render.
		/// </summary>
		public override UITableViewCell GetCell (UITableView tv)
		{
			var cell = tv.DequeueReusableCell (key) as PlaceDetailsCheckinCell;
			if (cell == null)
			{
				// We have to allocate a cell
				var views = NSBundle.MainBundle.LoadNib ("PlaceDetailsCheckinCell", tv, null);
				cell = Runtime.GetNSObject (views.ValueAt (0)) as PlaceDetailsCheckinCell;
			}
			
			cell.BindDataToCell (Place);
	
			return cell;
		}
	
		/// <summary>
		/// This function gets called when the returned cell is selected in the TableView
		/// </summary>
		public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
		{
		}
	
		/// <summary>
		/// Matches the specified text.
		/// </summary>
		/// <param name='text'>
		/// If set to <c>true</c> text.
		/// </param>
		public override bool Matches (string text)
		{
			return (Place.Name.IndexOf (text, StringComparison.CurrentCultureIgnoreCase) != -1);
		}
	
		public float GetHeight (UITableView tableView, NSIndexPath indexPath)
		{
			if (_cellHeight == 0) {
				var views = NSBundle.MainBundle.LoadNib ("PlaceDetailsCheckinCell", tableView, null);
				PlaceDetailsCheckinCell cell = Runtime.GetNSObject (views.ValueAt (0)) as PlaceDetailsCheckinCell;
					
				_cellHeight = cell.Bounds.Height;
			}
			
			return _cellHeight;
		}
	}
}

